/**
 * 构造器
 *
 * @author liu.bin
 */
function ZakeEvent() {

    // 容器面板
    this.nodeUserInfo      = "#user_info_panel";
    this.nodeFriendList    = "#friend_list_panel";
    this.nodeFriendInfo    = "#friend_info_panel_";


    // 模板
    this.tplUserInfo        = "#user_info_panel_tpl";
    this.tplFriendList      = "#friend_list_panel_tpl";
    this.tplChatSender      = "#chat_sender_panel_tpl";
    this.tplChatReceiver    = "#chat_receiver_panel_tpl";

    // 聊天面板
    this.chatUserBodyPanel  = "#chat_user_body_panel";
}


// 登陆成功+好友列表渲染
ZakeEvent.prototype.SLoginSuccess = function(body)
{

    // 更新用户信息
    var userInfo        = body.loginUser;
    var nodeUserInfo    = $(this.nodeUserInfo);
    userInfo.userJsonString = JSON.stringify(userInfo);

    if(undefined === userInfo || [] === userInfo){
        nodeUserInfo.html("<div>您已下线-1</div>");
        return;
    }

    // 渲染当前登陆用户
    var html = tpl.html($(this.tplUserInfo).text(),userInfo);
    nodeUserInfo.html(html);

    // 当前用户区
    var fromUserInput = $("#post_from_id");
    fromUserInput.val(userInfo.userJsonString);


    // 更新好友信息
    var nodeFriendList  = $(this.nodeFriendList);
    var friends         = body.friendList;
    if(null === friends || [] === friends || undefined === friends){
        nodeFriendList.html("<div>您还没有好友</div>");
        return;
    }


    var listHtml    = "";
    for(var i = 0;i < friends.length;i++){
        var friendUserInfo = friends[i];
        friendUserInfo.userJsonString = JSON.stringify(friends[i]);
        listHtml += tpl.html($(this.tplFriendList).text(),friendUserInfo);
    }

    //
    nodeFriendList.html(listHtml);

};



/**
 * 登陆失败
 * @param token
 * @param msg
 */
ZakeEvent.prototype.SLoginFail = function(token,msg)
{
    var nodeUserInfo    = $(this.nodeUserInfo);
    var nodeFriendList  = $(this.nodeFriendList);
    nodeUserInfo.html("<div>登陆失败</div><div>token:"+token+"</div>");
    nodeFriendList.html("原因：" + msg);
};



/**
 * 用户下线
 * @param
 */
ZakeEvent.prototype.SUserOff = function()
{
    var nodeUserInfo    = $(this.nodeUserInfo);
    var nodeFriendList  = $(this.nodeFriendList);

    nodeUserInfo.html("<div>您已下线</div>");
    nodeFriendList.html("");
};



/**
 * 好友上线
 * @param friendInfo
 */
ZakeEvent.prototype.SFriendOn = function(friendInfo)
{

    var userId              = friendInfo.id;
    var nodeFriendInfo      = $(this.nodeFriendInfo+userId);
    if(0 === nodeFriendInfo.length){
        alert("未找到该好友");
        return;
    }
    var userJsonString = JSON.stringify(friendInfo);

    // 同步好友信息
    nodeFriendInfo.find(".session_panel").html(friendInfo.sessionId);
    nodeFriendInfo.attr("data-userinfo",userJsonString);
};



/**
 * 好友下线
 * @param friendInfo
 */
ZakeEvent.prototype.SFriendOff = function(friendInfo)
{

    console.log("SFriendOff-1");

    var userId              = friendInfo.id;
    var nodeFriendInfo      = $(this.nodeFriendInfo+userId);
    if(0 === nodeFriendInfo.length){
        alert("未找到该好友");
        return;
    }
    nodeFriendInfo.find(".session_panel").html("0");
    nodeFriendInfo.attr("data-sessionid","0");
};


/**
 * 接收：私聊消息
 * @param messageDto
 */
ZakeEvent.prototype.messageP = function (messageDto) {

    // 聊天消息显示区
    var chatUserBodyPanel   = $(this.chatUserBodyPanel);
    // 接收消息模板
    var tplChatReceiverNode = $(this.tplChatReceiver);
    // 发送消息按钮
    var postBtn = $("#post_message_btn");
    // 消息输入区
    var messageInput = $("#post_message");
    // 发送用户区
    var toUserInput = $("#post_to_id");
    // 发送用户页面
    var chatTitlePanel = $("#chat_title_panel");

    // 消息来源用户
    var sendUser = messageDto.from;
    // 消息接收用户
    var revUser = messageDto.to;


    // 移除禁用
    messageInput.removeClass("disabled");
    postBtn.removeClass("disabled");

    // 接收消息html
    var html = tpl.html(tplChatReceiverNode.text(),messageDto);


    // 判断当前聊天用户
    var toChatUserVal = toUserInput.val();

    // 没有聊天用户
    if(toChatUserVal === 0 || toChatUserVal === "0"){

        // 没有聊天用户 || 当前聊天用户与发送用户一致
        toUserInput.val(JSON.stringify(sendUser));
        // title 显示聊天好友名称
        chatTitlePanel.html(sendUser.nickName);
        // 显示聊天消息
        chatUserBodyPanel.append(html);
        return true;

    }


    // 已有聊天用户,解析数据
    toChatUserVal = JSON.parse(toChatUserVal);

    if(toChatUserVal.id === sendUser.id){

        // 用户一致
        console.log("【+++++用户一致】");
        // 显示聊天消息
        chatUserBodyPanel.append(html);

    }else{


        // 用户不一致
        console.log("【------用户不一致】");
        console.log(toChatUserVal);
        console.log(sendUser);

        var toUserId = sendUser.id;
        var userPanel = $("#friend_info_panel_"+toUserId);
        var messageCount = parseInt(userPanel.find(".badge").text());
        var messagePanel = userPanel.find(".message_list_panel");

        // 未读消息 +1
        userPanel.find(".badge").html(messageCount + 1);

        // 缓存未读消息
        messagePanel.append(html);


    }



};


/**
 * 发送：私聊消息
 * @param messageDto
 */
ZakeEvent.prototype.postMessage = function (messageDto) {


    // 接收消息
    var tplChatReceiverNode = $(this.tplChatReceiver);
    var chatUserBodyPanel   = $(this.chatUserBodyPanel);

    var html = tpl.html(tplChatReceiverNode.text(),messageDto);
    chatUserBodyPanel.append(html);

};

